﻿package com.thorqq.magictimer.action;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Vector;

import android.content.Context;
import android.content.Intent;

import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.util.Util;

/**
 * @author THORQQ<br>
 *         定时动作管理器<br>
 *         使用方法:<br>
 *         ActionMgr.getIntance().getAction(int actionType).run();
 */
public class ActionMgr
{
    public static final String ACTION_KLAXON = "ACTION_KLAXON"; // 播放铃音
    public static final String ACTION_VIBRATOR = "ACTION_VIBRATOR"; // 震动
    public static final String ACTION_NOTIFY = "ACTION_NOTIFY"; // 通知栏提醒
    public static final String ACTION_DIALOG = "ACTION_DIALOG"; // 对话框提醒
    public static final String ACTION_AIRPLANE_MODE = "ACTION_AIRPLANE_MODE"; // 进入退出飞行模式
    public static final String ACTION_STOP_PROCESS = "ACTION_STOP_PROCESS"; // 清理内存
    public static final String ACTION_RINGER_MODE = "ACTION_RINGER_MODE"; // 调整铃音模式
    public static final String ACTION_VOLUME = "ACTION_VOLUME"; // 音量调节
    public static final String ACTION_SEND_SMS = "ACTION_SEND_SMS"; // 发送短信
    public static final String ACTION_SEND_VIRTUAL_SMS = "ACTION_SEND_VIRTUAL_SMS"; // 发送虚拟短信
    public static final String ACTION_RECV_VIRTUAL_SMS = "ACTION_RECV_VIRTUAL_SMS"; // 接收虚拟短信
    public static final String ACTION_PHONE = "ACTION_PHONE"; // 接收虚拟短信
    public static final String ACTION_DELETE_SMS = "ACTION_DELETE_SMS"; // 接收虚拟短信

    private static ActionMgr mActionMgr;
    
    private HashMap<String, Class<?>> mActionMap = new HashMap<String, Class<?>>();
    private HashMap<String, Class<?>> mChildViewMap = new HashMap<String, Class<?>>();

    public static ActionMgr getIntance()
    {
        if (mActionMgr == null)
        {
            mActionMgr = new ActionMgr();
        }
        return mActionMgr;
    }

    public void init()
    {
        registerAction(ACTION_KLAXON, ActionKlaxon.class);
        registerAction(ACTION_VIBRATOR, ActionVibrator.class);
        registerAction(ACTION_NOTIFY, ActionNotify.class);
        registerAction(ACTION_DIALOG, ActionDialog.class);
        registerAction(ACTION_AIRPLANE_MODE, ActionAirplaneMode.class);
        registerAction(ACTION_STOP_PROCESS, ActionStopProcess.class);
        registerAction(ACTION_RINGER_MODE, ActionRingerMode.class);
        registerAction(ACTION_VOLUME, ActionVolume.class);
        registerAction(ACTION_SEND_SMS, ActionSendSMS.class);
        registerAction(ACTION_SEND_VIRTUAL_SMS, ActionSendVirtualSMS.class);
        registerAction(ACTION_RECV_VIRTUAL_SMS, ActionRecvVirtualSMS.class);
        registerAction(ACTION_PHONE, ActionPhone.class);
        registerAction(ACTION_DELETE_SMS, ActionDeleteSMS.class);

        registerChildView(ACTION_VOLUME, ActionVolumeChildView.class);
        registerChildView(ACTION_SEND_SMS, ActionSendSMSChildView.class);
        registerChildView(ACTION_SEND_VIRTUAL_SMS, ActionSendSMSChildView.class);
        registerChildView(ACTION_RECV_VIRTUAL_SMS, ActionRecvVirtualSMSChildView.class);
    }

    public Object registerAction(String actionType, Class<?> action)
    {
        return mActionMap.put(actionType, action);
    }

    public Object registerChildView(String actionType, Class<?> childView)
    {
        return mChildViewMap.put(actionType, childView);
    }

    public TAction createAction(String actionType)
    {
        int id = -1;
        int execOrder = -1;
        String param = null;
        return createAction(id, execOrder, actionType, param);
    }

    public TAction createAction(String actionType, String param)
    {
        int id = -1;
        int execOrder = -1;
        return createAction(id, execOrder, actionType, param);
    }

    public TAction createAction(int id, int execOrder, String actionType, String param)
    {
        Class<?> c = mActionMap.get(actionType);
        TAction action = null;
        if (c == null)
        {
            Util.log("Error action type : " + actionType);
            return null;
        }
        
        Constructor<?> localConstructor;
        try
        {
            Class<?>[] ctorArgs4 = new Class[4];
            ctorArgs4[0] = int.class;
            ctorArgs4[1] = int.class;
            ctorArgs4[2] = String.class;
            ctorArgs4[3] = String.class;
            localConstructor = c.getConstructor(ctorArgs4);
            action = (TAction) localConstructor.newInstance(id, execOrder, actionType, param);
        } catch (Exception e)
        {
            Util.log_ex(e);
        }
        
        //解析参数字符串
        if(!action.parseStringParam())
        {
            Util.log(action.getDescription() + " parseStringParam failed. " + action.toString());
            return null;
        }         
        
        return action;
    }

    public ChildView createChildView(Context context, TAction action)
    {
        Class<?> c = mChildViewMap.get(action.getActionType());
        ChildView childView = null;
        if (c != null)
        {
            Constructor<?> localConstructor;
            try
            {
                Class<?>[] ctorArgs2 = new Class[2];
                ctorArgs2[0] = Context.class;
                ctorArgs2[1] = TAction.class;
                localConstructor = c.getConstructor(ctorArgs2);
                childView = (ChildView) localConstructor.newInstance(context, action);
            } catch (Exception e)
            {
                Util.log_ex(e);
            }
        }

        return childView;
    }

    // intent与服务
    private Vector<String> mActionArray = new Vector<String>();

    public void registerSvc(String intent_action)
    {
        mActionArray.add(intent_action);
    }

    public void stopAllSvc(Context context)
    {
        for (String s : mActionArray)
        {
            context.stopService(new Intent(s));
        }
    }
}
